package cn.ym.mall.service.impl;

import cn.ym.mall.dao.StaffDao;
import cn.ym.mall.dao.impl.StaffDaoImpl;
import cn.ym.mall.entity.Staff;
import cn.ym.mall.service.StaffService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;


public class StaffServiceImpl implements StaffService {
    StaffDao staffDao = new StaffDaoImpl();
    /**
     * 分页查询所有员工
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Staff> getAll(Integer pageNo, Integer pageSize) throws SQLException {
        Connection conn = null;
        Page<Staff> staffPage = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            staffPage.setPageNo(pageNo);
            staffPage.setPageSize(pageSize);
            staffPage.setCount(Math.toIntExact(Long.valueOf(staffDao.getCount())));//long转int类型转换
            staffPage.setList(staffDao.getAll(pageNo,pageSize));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staffPage;
    }

    /**
     * 添加新员工
     *
     * @param staff
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(Staff staff) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (staffDao.add(staff) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 员工编号移除员工
     *
     * @param stid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean delete(Integer stid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (staffDao.delete(stid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 更新员工信息
     *
     * @param staff
     * @return
     * @throws SQLException
     */
    @Override
    public boolean update(Staff staff) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (staffDao.update(staff) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 员工姓名查询员工信息
     *
     * @param stname
     * @return
     * @throws SQLException
     */
    @Override
    public Staff getStaffByStname(String stname) throws SQLException {
        Connection conn = null;
        Staff staff = null;
        try {
            conn = DruidUtils.getConnection();
            staff = staffDao.getStaffByStname(stname);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staff;
    }

    /**
     * 根据角色id查询员工
     *
     * @param stid
     * @return
     * @throws SQLException
     */
    @Override
    public Staff getStaffByStid(Integer stid) throws SQLException {
        Connection conn = null;
        Staff staff = null;
        try {
            conn = DruidUtils.getConnection();
            staff = staffDao.getStaffByStid(stid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staff;
    }

    /**
     * 登录验证
     *
     * @param staccount
     * @return
     * @throws SQLException
     */
    @Override
    public Staff getStaffByStaccount(String staccount) throws SQLException {
        Connection conn = null;
        Staff staff = null;
        try {
            conn = DruidUtils.getConnection();
            staff = staffDao.getStaffByStaccount(staccount);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staff;
    }

    /**
     * 根据角色id查询员工
     *
     * @param rid
     * @param PageNo
     * @param PageSize
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Staff> getStaffByRid(Integer rid, Integer PageNo, Integer PageSize) throws SQLException {
        Connection conn = null;
        Page<Staff> staffPage = new Page<>();
        try {
            conn = DruidUtils.getConnection();
            staffPage.setPageNo(PageNo);
            staffPage.setPageSize(PageSize);
            staffPage.setCount(Math.toIntExact(Long.valueOf(staffDao.getCount())));//long转int类型转换
            staffPage.setList(staffDao.getStaffByRid(rid,PageNo,PageSize));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return staffPage;
    }

}
